package com.cskaoyan.controller.admin;

import com.cskaoyan.bean.MarketOrder;
import com.cskaoyan.bean.MarketOrderChannel;
import com.cskaoyan.bean.bo.admin.order.MarketOrderSkipBo;
import com.cskaoyan.bean.bo.admin.order.OrderListBO;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.vo.admin.order.MarketOrderDetailVo;
import com.cskaoyan.service.admin.MarketOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Description
 * @Author klw
 * @Data 2022/11/21 下午2:39
 */

@RestController
@RequestMapping("admin/order")
public class MarketOrderController {

    @Autowired
    MarketOrderService marketOrderService;

    @GetMapping("list")
    public BaseRespVo list(OrderListBO orderListBO, BasePageInfo basePageInfo) throws ParseException {
        CommonData<MarketOrder> query = marketOrderService.query(orderListBO, basePageInfo);
        return BaseRespVo.ok(query);
    }

    @RequestMapping("channel")
    public BaseRespVo<List<MarketOrderChannel>> channel() {
        ArrayList<MarketOrderChannel> marketOrderChannels = new ArrayList<>();
        marketOrderChannels.add(new MarketOrderChannel("ZTO", "中通快递"));
        marketOrderChannels.add(new MarketOrderChannel("YTO", "圆通快递"));
        marketOrderChannels.add(new MarketOrderChannel("YD", "韵达快递"));
        marketOrderChannels.add(new MarketOrderChannel("YZPY", "邮政快递包裹"));
        marketOrderChannels.add(new MarketOrderChannel("EMS", "EMS"));
        marketOrderChannels.add(new MarketOrderChannel("DBL", "德邦快递"));
        marketOrderChannels.add(new MarketOrderChannel("FAST", "快捷快递"));
        marketOrderChannels.add(new MarketOrderChannel("ZJS", "宅急送"));
        marketOrderChannels.add(new MarketOrderChannel("TNT", "TNT快递"));
        marketOrderChannels.add(new MarketOrderChannel("UPS", "UPS"));
        marketOrderChannels.add(new MarketOrderChannel("DHL", "DHL"));
        marketOrderChannels.add(new MarketOrderChannel("FEDEX", "FEDEX联邦(国内件"));
        marketOrderChannels.add(new MarketOrderChannel("FEDEX_G", "FEDEX联邦(国际件"));
        return BaseRespVo.ok(marketOrderChannels);
    }

    /**
     * 订单详情信息
     *
     * @param id 订单id
     * @return BaseRespVo<MarketOrderDetailVo>
     */
    @RequestMapping("detail")
    public BaseRespVo<MarketOrderDetailVo> detail(String id) {
        MarketOrderDetailVo data = marketOrderService.detail(id);
        return BaseRespVo.ok(data);
    }

    /**
     * 发货
     * @param marketOrderSkipBo 要发货的信息
     * @return ok
     */
    @RequestMapping("ship")
    public BaseRespVo<Object> ship(@RequestBody MarketOrderSkipBo marketOrderSkipBo){
        try {
            long shipSn = Long.parseLong(marketOrderSkipBo.getShipSn());
        } catch (NumberFormatException e) {
            e.printStackTrace();
            return BaseRespVo.error("参数非法");
        }
        marketOrderService.ship(marketOrderSkipBo);
        return BaseRespVo.ok();
    }

    /**
     * 删除订单
     * @param   map 接收订单id
     * @return ok
     */
    @RequestMapping("delete")
    public BaseRespVo<Object> delete(@RequestBody Map<String,String> map){
        String orderId = map.get("orderId");
        int affectedRows = marketOrderService.delete(orderId);
        if(affectedRows==0){
            return BaseRespVo.error("订单不能删除");
        }
        return BaseRespVo.ok();
    }


}
